<?php
/**
 * UserController.php
 * @author 瀚海浪亭
 * @email  1171672036@qq.com
 * @date   2021/4/19
 */

namespace app\api\controller;


use app\api\validate\UserValidate;
use app\common\exception\DbException;
use app\common\exception\RequestException;
use app\common\model\UserModel;
use think\facade\Request;
use Tools\Json;
use Tools\Responses;

class UserController extends ApiBaseController
{
    /**
     * 数据列表
     *
     * @return Json
     * */
    public function lists()
    {

        $UserModel = new UserModel();

        //处理过滤
        $filters = Request::post('filters', []);
        if ($filters && is_array($filters)) {
            foreach ($filters as $name => $value) {
                switch ($name) {
                    case 'ids':
                        if ($value && is_array($value)) {
                            $UserModel = $UserModel->where('id', 'in', $value);
                        }
                        break;
                }
            }
        }

        $page_size = Request::post('page_size', env('page.size', 10));

        $lists = $UserModel->paginate($page_size)->toArray();

        $data = $lists['data'];

        //组装分页
        $pages = get_list_pages($lists);
        return Responses::data(200, 'success', $data, $pages);

    }

    /**
     * 数据详情
     *
     * @return Responses
     * */
    public function detail()
    {
        $params['id'] = $this->user_id;
        $validate     = new UserValidate();
        $vResult      = $validate->scene(__FUNCTION__)->check($params);
        if (!$vResult) {
            throw new RequestException(400, $validate->getError());
        }

        $data = UserModel::where('id', '=', $params['id'])->field('id, username, avatar, phone, email, last_login_time, last_login_ip, status')->find();
        if (!$data) {
            throw new RequestException(404, '用户不存在');
        }

        return Responses::data(200, '请求成功', $data);
    }

    /**
     * 创建数据
     *
     * @return Responses
     * */
    public function create()
    {

        //表单验证
        $validate = new UserValidate();
        $vResult  = $validate->scene(__FUNCTION__)->check(Request::post());
        if (!$vResult) {
            throw new RequestException($validate->getError(), 40003);
        }

        $data           = [];
        $data['title']  = Request::post('title', '', 'trim,strip_tags,htmlspecialchars');
        $data['sort']   = Request::post('sort', 0, 'intval');
        $data['status'] = 2;

        try {

            $info = UserModel::create($data);
            if (!$info) {
                throw new DbException('数据添加失败', 50001);
            }

            return Responses::data(200, 'success', ['id' => $info->id]);

        } catch (DbException $e) {
            return Responses::data(50001, $e->getMessage());
        }

    }

    /**
     * 更新数据
     *
     * @return Responses
     * */
    public function update()
    {

        //表单验证
        $validate = new UserValidate();
        $vResult  = $validate->scene(__FUNCTION__)->check(Request::post());
        if (!$vResult) {
            throw new RequestException($validate->getError(), 40003);
        }

        $id   = Request::post('id');
        $info = UserModel::where('id', '=', $id)->find();
        if (!$info) {
            throw new RequestException('数据不存在', 40401);
        }

        $data          = [];
        $data['title'] = Request::post('title', '', 'trim,strip_tags,htmlspecialchars');
        $data['sort']  = Request::post('sort', 0, 'intval');

        try {

            $result = UserModel::where('id', '=', $id)->update($data);
            if (!$result) {
                throw new DbException('数据更新失败', 50001);
            }

            return Responses::data(200, 'success');

        } catch (DbException $e) {
            return Responses::data(50001, $e->getMessage());
        }

    }

    /**
     * 删除数据
     *
     * @return Responses
     * */
    public function delete()
    {

        //表单验证
        $validate = new UserValidate();
        $vResult  = $validate->scene(__FUNCTION__)->check(Request::post());
        if (!$vResult) {
            throw new RequestException($validate->getError(), 40003);
        }

        $id = Request::post('id');

        try {
            UserModel::where('id', '=', $id)->delete();
        } catch (DbException $e) {
            throw new DbException('删除失败', 50001);
        }

        return Responses::data(200, 'success');
    }
}